﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ChequeManagement.BLL
{
    /// <summary>
    /// This Class contains number of Field, constructor, method and this class inheritance of BaseFactory clas that Inheritance of "BaseObject" Class
    /// </summary>
    /// <properties>this properties contain set for receive value and contain get for return value </properties>

    public class BLLPayAndReceive : BaseObject
    {

        #region Fields

        /// <summary>
        /// This enum Name is "PayAndReceiveField"  that includes PayAndReceiveID, Date, Description, DrawerID, Type
        /// </summary>
        public enum PayAndReceiveField
        {
            PayAndReceiveID,
            Date,
            Description,
            DrawerID,
            Type
        }

        //define nullable version of int
        private int? payAndReceiveID;

        //define nullable version of DateTime
        private DateTime? date;

        private string description = null;
        private BLLDrawer drawerObj = null;
        
        //it use only for reading. with this then we don't need to create object of the Drawer to fill the drawerID
        private int? drawerID;

        //define nullable version of DocumentType
        private DocumentType? type;

        private List<BLLPayAndReceiveItem> payAndReceiveItemList = new List<BLLPayAndReceiveItem>();

        #endregion

        #region Property

        /// <summary>
        /// This property contain set for receive value and contain get for return value
        /// </summary>
        public int PayAndReceiveID
        {
            get
            {
                return payAndReceiveID.Value;
            }

            set
            {
                payAndReceiveID = value;
            }
        }

        /// <summary>
        /// This property contain set for receive value and contain get for return value
        /// </summary>
        public DateTime Date
        {
            get
            {
                return date.Value;
            }
            set
            {
                date = value;
            }
        }

        /// <summary>
        ///This property contain set for receive value and contain get for return value
        /// </summary>
        public string Description
        {
            get;
            set;
        }

        /// <summary>
        ///This property contain set for receive value and contain get for return value
        /// </summary>
        public int DrawerID
        {
            get
            {
                return drawerID.Value;
            }
            set
            {
                drawerID = value;
            }
        }

        /// <summary>
        /// This property contain set for receive value and contain get for return value
        /// </summary>
        public BLLDrawer DrawerObj
        {
            get
            {
                return drawerObj;
            }
            set
            {
                drawerObj = value;
            }
        }

        /// <summary>
        /// This property contain set for receive value and contain get for return value
        /// </summary>
        public DocumentType Type
        {
            get
            {
                return type.Value;
            }
            set
            {
                type = value;
            }
        }

        /// <summary>
        /// This property contain set for receive value that set is private and contain get for return value
        /// </summary>
        public List<BLLPayAndReceiveItem> BLLPayAndReceiveItemList
        {
            get
            {
                return payAndReceiveItemList;
            }
            private set
            {
                payAndReceiveItemList = value;
            }
        }


        #endregion

        #region Method
        /// <summary>
        /// This constructor not have parameter and use on create instance of class 
        /// </summary>
        public BLLPayAndReceive()
        {
        }

        /// <summary>
        /// This method is for createobject of "BLLPayAndReceive" and this method is static
        /// </summary>
        /// <returns>object that type is BLLPayAndReceive</returns>
        public static BLLPayAndReceive CreateObject()
        {
            return new BLLPayAndReceive();
        }

        /// <summary>
        /// This method is for add instance of "BLLPayAndReceiveItem"
        /// </summary>
        /// <param name="chequeNo" Type="long"></param>
        /// <param name="accountNo" Type="long"></param>
        /// <param name="bankObject" Type="BLL.BLLBank"></param>
        /// <param name="amount" Type="decimal"></param>
        /// <param name="dateOfIssue" Type="DateTime"></param>
        /// <param name="branchName" Type="string"></param>
        /// <param name="payee" Type="string"></param>
        /// <param name="status" Type="ChequeStatus">This parameter type is enum</param>
        /// <return>this type is void and Do not return</return>
        public void AddBLLPayAndReceiveItem(long chequeNo, long accountNo, BLL.BLLBank bankObject, decimal amount, DateTime dateOfIssue, string branchName, string payee, ChequeStatus status)
        {
            if (payAndReceiveID.HasValue)
                payAndReceiveItemList.Add(new BLLPayAndReceiveItem(chequeNo, accountNo, bankObject, amount, dateOfIssue, branchName, payee, status, payAndReceiveID.Value));
            else
                throw new ArgumentNullException("payAndReceiveID is null! please fill it at first then create BLLPayAndReceiveItem");

        }

        /// <summary>
        /// This method check fields value
        /// </summary>
        /// <param name="excepts" Type="string[]"></param>
        /// <date>if date value is null this method add error</date>
        /// <drawerObj>if drawerObj value is null this method add error</drawerObj>
        /// <type>if type value is null this method add error</type>
        /// <payAndReceiveItemList>if payAndReceiveItemList value is null this method add error,payAndReceiveItemList is a list<> and type is BLLPayAndReceiveItem </payAndReceiveItemList>
        /// <returns>if all field is Valid,ErrorsCount value is zero and return true else ErrorsCount value Larger of zero and return false</returns></returns>
        public bool IsValid(params string[] excepts)
        {
            Validation.ValidationRule vr = new Validation.ValidationRule();

            if (!date.HasValue)
            {
                AddError("تاریخ وارد نشده است", null);
            }
            else
            {

            }

            if (drawerObj == null)
            {
                AddError("ابتدا باید طرف حساب را معرفی کنید", null);
            }
            else
            {

            }

            if (!type.HasValue)
            {
                AddError("نوع سند مشخص نشده است", null);
            }
            else
            {

            }

            if (payAndReceiveItemList == null)
            {
                AddError("حداقل باید یک سند پرداختی و یا دریافتی تعریف کنید", null);
            }
            else
            {

            }

            string message;
            vr.DefaultMessageForNameIsValid = Validation.ValidationRule.DefaultForNameIsValid.Description;
            if (!vr.NameIsValid(Description, 0, 10, out message))
            {
                AddError(message, null);
            }

            if (ErrorsCount > 0)
                return false;
            else
                return true;
        }

        #endregion

    }
}
